package com.main.gateway.interfaces.controller;

import com.main.common.base.resp.RespBody;
import com.main.common.utils.common.RedisUtils;
import com.main.gateway.domain.entity.model.User;
import com.main.gateway.application.service.UserService;
import io.github.resilience4j.bulkhead.annotation.Bulkhead;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtils redisUtils;


    @GetMapping("/user")
    @Bulkhead(name = "bulkhead",fallbackMethod = "myBulkheadPoolFallback")
//    @Bulkhead(name = "ThreadPoolBulkhead",fallbackMethod = "myBulkheadPoolFallback",type = Bulkhead.Type.THREADPOOL)
    public RespBody<User> getUserById(Integer Id) {
        if(Id == null) return null;
        redisUtils.set("aaa", Id);
        return RespBody.ok(userService.getUserById(Id));
    }

    public RespBody<User> myBulkheadPoolFallback(Integer Id, Throwable t) {
        System.out.println("TOO_MANY_REQUESTS");
//      throw new BusinessException(RespCode.TOO_MANY_REQUESTS);
        return RespBody.ok(userService.getUserById(Id));
    }

}